package com.cv4j.app.slice;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Image;
import ohos.agp.components.element.PixelMapElement;
import ohos.media.image.PixelMap;

import com.cv4j.app.ResourceTable;
import com.cv4j.app.app.BaseAbilitySlice;
import com.cv4j.core.datamodel.CV4JImage;
import com.cv4j.core.datamodel.ImageProcessor;
import com.cv4j.core.pixels.Flip;

/**
 * FlipAbilitySlice
 *
 * @since 2021-05-14
 */
public class FlipAbilitySlice extends BaseAbilitySlice {
    private Image image;
    private Image resultimage;
    private Image resultimage1;

    /**
     * 获取布局id
     *
     * @return 布局id
     */
    protected int getUIContentLayoutId() {
        return ResourceTable.Layout_ability_flip;
    }

    /**
     * 操作类
     *
     * @param intent 意图
     */
    protected void initComponents(Intent intent) {
        initView();
        initData();
    }

    /**
     * initData
     */
    private void initData() {
        image.setPixelMap(ResourceTable.Media_pixel_test_1);
        CV4JImage cv4jImage = new CV4JImage(image.getPixelMap());
        ImageProcessor imageProcessor = cv4jImage.getProcessor();
        Flip.flip(imageProcessor, Flip.FLIP_HORIZONTAL);
        if (imageProcessor != null) {
            CV4JImage resultcv4jImage = new CV4JImage(imageProcessor.getWidth(),
                imageProcessor.getHeight(), imageProcessor.getPixels());
            PixelMap pixelMap = resultcv4jImage.getProcessor().getImage().toBitmap();
            resultimage.setImageElement(new PixelMapElement(pixelMap));
        }
        cv4jImage = new CV4JImage(image.getPixelMap());
        ImageProcessor imageProcessor2 = cv4jImage.getProcessor();
        Flip.flip(imageProcessor2, Flip.FLIP_VERTICAL);
        if (imageProcessor2 != null) {
            CV4JImage resultcv4jImage = new CV4JImage(imageProcessor2.getWidth(),
                imageProcessor.getHeight(), imageProcessor2.getPixels());
            PixelMap pixelMap = resultcv4jImage.getProcessor().getImage().toBitmap();
            resultimage1.setImageElement(new PixelMapElement(pixelMap));
        }
    }

    /**
     * initView
     */
    private void initView() {
        image = (Image) findComponentById(ResourceTable.Id_flip_image);
        resultimage = (Image) findComponentById(ResourceTable.Id_flip_image1);
        resultimage1 = (Image) findComponentById(ResourceTable.Id_flip_image2);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
